/**************************************************************************/
/*! 
    @file     ds1307.h
    @author   K. Townsend (microBuilder.eu)

    @section LICENSE

    Software License Agreement (BSD License)

    Copyright (c) 2010, microBuilder SARL
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
// adapted from isl12022 to DS1307

#ifndef _DS1307_H_
#define _DS1307_H_

#include "../src/projectconfig.h"
#include "../core/i2c.h"

// DS1307 address

#define DS1307_RTC_ADDRESS       (0xD0)    // 1101111 shifted left 1 bit = 0xDE
#define DS1307_READBIT           (0x01)


#define DS1307_CLOCK_HALT        (1<<7)    // CH bit
#define DS1307_OUT               (1<<7)    // OUT bit
#define DS1307_SQWE              (1<<4)    // SQWE bit
#define DS1307_RS1               (1<<1)    // RS1 bit
#define DS1307_RS0               (1<<0)    // RS0 bit
#define DS1307_HR_MILITARY       (1<<6)    // 1 = 24-Hour, 0 = 12-Hour

enum
{
  // RTC Registers
  DS1307_REG_RTC_SC               = 0x00,
  DS1307_REG_RTC_MN               = 0x01,
  DS1307_REG_RTC_HR               = 0x02,
  DS1307_REG_RTC_DW               = 0x03,
  DS1307_REG_RTC_DT               = 0x04,
  DS1307_REG_RTC_MO               = 0x05,
  DS1307_REG_RTC_YR               = 0x06,
  // Control and Status Registers
  DS1307_REG_CSR_CR               = 0x07,
  // General Purpose Memory (SRAM) Registers
  DS1307_REG_GPM_GPM              = 0x08,
};

typedef enum
{
  DS1307_ERROR_OK = 0,               // Everything executed normally
  DS1307_ERROR_I2C_INIT,             // Unable to initialise I2C
  DS1307_ERROR_I2C_BUSY,             // I2C already in use
  DS1307_ERROR_I2C_BUFFEROVERFLOW,   // I2C Buffer is too small
  DS1307_ERROR_RTC_LOWBATT,          // RTC battery low ... value may not be accurate
  DS1307_ERROR_LAST
}
ds1307Error_t;

// DS1307 time + status placeholder
typedef struct
{
  uint8_t dayofweek;
  uint8_t year;
  uint8_t month;
  uint8_t day;
  uint8_t hour;
  uint8_t minute;
  uint8_t second;
} 
ds1307Time_t;

ds1307Error_t ds1307Init(void);
ds1307Error_t ds1307GetTime(ds1307Time_t *time);
ds1307Error_t ds1307SetTime(uint8_t year, uint8_t month, uint8_t day, uint8_t dayofweek, uint8_t hour, uint8_t minute, uint8_t second);
ds1307Error_t ds1307GetTemp(uint8_t *celsius);

#endif
